﻿
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
namespace UIFrame
{
    /// <summary>
    /// 此框架所有UI面板都先放到场景里面
    /// </summary>
    public class UIManager : MonoBehaviour
    {
        private static UIManager _instance;
        public static UIManager Instance { get => _instance; }
        public void Awake()
        {
            if (_instance == null)
                _instance = this;
            InitPanel();

            CloseAllPanel();            
        }

        public void Start()
        {
            ShowPanel(nameof(MainPanel));
        }
     
        public List<BasePanel> listManualPanels;
        //所有的UI面板
        private Dictionary<string, BasePanel> _dicPanel = new Dictionary<string, BasePanel>();
        //当前普通面板调用记录栈
        private Stack<BasePanel> _topPanel = new Stack<BasePanel>();


        /// <summary>
        /// 使用重置来刷新所有的面板
        /// </summary>
        public void Reset()
        {
            listManualPanels.Clear();
            listManualPanels.AddRange(Resources.FindObjectsOfTypeAll<BasePanel>());
        }

        private void InitPanel()
        {
            if (listManualPanels.Count == 0)
                Debug.LogError("请在UIManager配置用到的UI面板");

            foreach (var item in listManualPanels)
            {
                _dicPanel.Add(item.name, item);
            }
        }

        /// <summary>
        /// 打开Normal的UI，同时会关闭其他已打开的NormalUI
        /// </summary>
        /// <param name="panelName"></param>
        public void ShowPanel(string panelName)
        {
            //检测面板是否存在
            if (!_dicPanel.ContainsKey(panelName))
            {
                Debug.LogError("没有找到：" + panelName + "请检测输入是否正确");
                return;
            }
            CloseAllPanel();
            _dicPanel[panelName].Show();

            //压到栈底
            _topPanel.Push(_dicPanel[panelName]);
        }


        /// <summary>
        /// 关闭所有的NormalUI;
        /// </summary>
        public void CloseAllPanel()
        {
            foreach (var item in listManualPanels)
            {
                item.Close();
            }
        }

        public void BackLastPanel()
        {
            if (_topPanel.Count > 1)
            {
                _topPanel.Pop();
            }
            //弹一个栈
            string name = _topPanel.Pop().gameObject.name;
            ShowPanel(name);
        }

        /// <summary>
        /// 黑名单panel返回用
        /// </summary>
        public void BlackPanelBackLastPanel()
        {
            string name = _topPanel.Pop().gameObject.name;
            ShowPanel(name);
        }

        /// <summary>
        /// 弹出提示面板
        /// </summary>
        /// <param name="str">提示内容</param>
        /// <param name="callback">关闭消息面板的回调</param>
        public void ShowMessage(string str, UnityAction callback)
        {
            MessagePanel msg = _dicPanel["MessagePanel"] as MessagePanel;
            msg.ShowMessage(str, callback);
            msg.Show();
        }

        /// <summary>
        /// 密码验证
        /// </summary>
        /// <param name="callback">取消密码输入事件（返回）</param>
        /// <param name="sueecss">密码验证成功事件（下一个）</param>
        public void ShowPassword(UnityAction sueecss, UnityAction callback)
        {
            PasswordPanel pass = _dicPanel["PasswordPanel"] as PasswordPanel;
            pass.ReisgerPasswordAction(sueecss, callback);
            pass.Show();
        }


    }
}

